(function() {
  'use strict';
  var ns = window.beneath = window.beneath || {};
  
  function Preloader() {
    this.asset = null;
    this.ready = false;
  }

  Preloader.prototype = {

    preload: function () {

      this.load.spritesheet('gfx', 'assets/gfx.png', 16, 19);
      this.load.spritesheet('paper', 'assets/gfx.png', 16 * 5, 19 * 6);
      this.load.spritesheet('desk', 'assets/gfx.png', 16 * 8, 19 * 4);
      this.load.spritesheet('door', 'assets/gfx.png', 16 * 2, 19 * 3);
      this.load.bitmapFont('font', 'assets/font.png', 'assets/font.xml');
      this.load.bitmapFont('font_black', 'assets/font_black.png', 'assets/font.xml');

      this.load.audio('hit0', 'assets/sounds/mineHit.wav');
      this.load.audio('hit1', 'assets/sounds/mineHit1.wav');
      this.load.audio('hit2', 'assets/sounds/mineHit2.wav');



      this.load.audio('speak', 'assets/sounds/speak.wav');
      this.load.audio('coal', 'assets/sounds/coalpickup.wav');
      this.load.audio('break', 'assets/sounds/breakwall.wav');



      this.load.audio('sell0', 'assets/sounds/sell.wav');
      this.load.audio('sell1', 'assets/sounds/sell1.wav');

      this.load.spritesheet('sml', 'assets/projectiles.png', 10, 10);
      
      this.asset = this.add.sprite(document.body.clientWidth / 2, document.body.clientHeight / 2, 'preloader');
      this.asset.anchor.setTo(0.5, 0.5);

      this.load.onLoadComplete.addOnce(this.onLoadComplete, this);
      this.load.setPreloadSprite(this.asset);


      
    },

    create: function () {
      this.asset.cropEnabled = false;
    },

    update: function () {
      if (!!this.ready) {
        this.game.state.start('office', true, false, [{
          day : 0,
          firstDay : true,
          money : 10000,
          morale : 100,
          coal : [],
          employees : [
            ns.Miner.randomMiner(),
            ns.Miner.randomMiner(),
            ns.Miner.randomMiner()
          ],
          employeeSalery : 100,
          maintainance : 500,
          favors : 0,
          haveUnion : false,
          maxCommands : 9999,
          minerDied : []
        }]);
      }
    },

    onLoadComplete: function () {
      this.ready = true;
    }


  };

  window.beneath.Preloader = Preloader;

}());
